package gld;

import java.awt.geom.Point2D;

import static robocode.util.Utils.*;

public class WaveBullet {

	private Calculation calc = new Calculation();

	private double startX;
	private double startY;
	private double startBearing;
	private double power;
	private long fireTime;
	private int direction;
	private int[] returnSegment;

	public WaveBullet(double x, double y, double bearing, double power, int direction, long time, int[] segment) {
		startX = x;
		startY = y;
		startBearing = bearing;
		this.power = power;
		this.direction = direction;
		fireTime = time;
		returnSegment = segment;
	}

	public boolean checkHit(double targetX, double targetY, long currentTime) {
		if (Point2D.distance(startX, startY, targetX, targetY) <= (currentTime - fireTime) * calc.bulletSpeed(power)) {
			double desiredDirection = Math.atan2(targetX - startX, targetY - startY);
			double angleOffset = normalRelativeAngle(desiredDirection - startBearing);
			double guessFactor = Math.max(-1, Math.min(1, angleOffset / calc.maxEscapeAngle(power))) * direction;
			int index = (int) Math.round((returnSegment.length - 1) / 2 * (guessFactor + 1));
			returnSegment[index]++;
			return true;
		}
		return false;
	}

}
